#ifndef THINK_THINK_CONTROLLER_REST_H
#define THINK_THINK_CONTROLLER_REST_H

static char* think_controller_rest =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think\\controller;\n"
	"\n"
	"use think\\App;\n"
	"use think\\Request;\n"
	"use think\\Response;\n"
	"\n"
	"abstract class Rest\n"
	"{\n"
	"\n"
	"    protected $method; // 当前请求类型\n"
	"    protected $type; // 当前资源类型\n"
	"    // 输出类型\n"
	"    protected $restMethodList    = \'get|post|put|delete\';\n"
	"    protected $restDefaultMethod = \'get\';\n"
	"    protected $restTypeList      = \'html|xml|json|rss\';\n"
	"    protected $restDefaultType   = \'html\';\n"
	"    protected $restOutputType    = [ // REST允许输出的资源类型列表\n"
	"        \'xml\'  => \'application/xml\',\n"
	"        \'json\' => \'application/json\',\n"
	"        \'html\' => \'text/html\',\n"
	"    ];\n"
	"\n"
	"    /**\n"
	"     * 构造函数 取得模板对象实例\n"
	"     * @access public\n"
	"     */\n"
	"    public function __construct()\n"
	"    {\n"
	"        // 资源类型检测\n"
	"        $request = Request::instance();\n"
	"        $ext     = $request->ext();\n"
	"        if (\'\' == $ext) {\n"
	"            // 自动检测资源类型\n"
	"            $this->type = $request->type();\n"
	"        } elseif (!preg_match(\'/(\' . $this->restTypeList . \')$/i\', $ext)) {\n"
	"            // 资源类型非法 则用默认资源类型访问\n"
	"            $this->type = $this->restDefaultType;\n"
	"        } else {\n"
	"            $this->type = $ext;\n"
	"        }\n"
	"        // 请求方式检测\n"
	"        $method = strtolower($request->method());\n"
	"        if (!preg_match(\'/(\' . $this->restMethodList . \')$/i\', $method)) {\n"
	"            // 请求方式非法 则用默认请求方法\n"
	"            $method = $this->restDefaultMethod;\n"
	"        }\n"
	"        $this->method = $method;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * REST 调用\n"
	"     * @access public\n"
	"     * @param string $method 方法名\n"
	"     * @return mixed\n"
	"     * @throws \\Exception\n"
	"     */\n"
	"    public function _empty($method)\n"
	"    {\n"
	"        if (method_exists($this, $method . \'_\' . $this->method . \'_\' . $this->type)) {\n"
	"            // RESTFul方法支持\n"
	"            $fun = $method . \'_\' . $this->method . \'_\' . $this->type;\n"
	"        } elseif ($this->method == $this->restDefaultMethod && method_exists($this, $method . \'_\' . $this->type)) {\n"
	"            $fun = $method . \'_\' . $this->type;\n"
	"        } elseif ($this->type == $this->restDefaultType && method_exists($this, $method . \'_\' . $this->method)) {\n"
	"            $fun = $method . \'_\' . $this->method;\n"
	"        }\n"
	"        if (isset($fun)) {\n"
	"            return App::invokeMethod([$this, $fun]);\n"
	"        } else {\n"
	"            // 抛出异常\n"
	"            throw new \\Exception(\'error action :\' . $method);\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 输出返回数据\n"
	"     * @access protected\n"
	"     * @param mixed     $data 要返回的数据\n"
	"     * @param String    $type 返回类型 JSON XML\n"
	"     * @param integer   $code HTTP状态码\n"
	"     * @return Response\n"
	"     */\n"
	"    protected function response($data, $type = \'json\', $code = 200)\n"
	"    {\n"
	"        return Response::create($data, $type)->code($code);\n"
	"    }\n"
	"\n"
	"}\n"
	"\n"
;
#endif